/**
 *====================================================
 * 文件名称: BulkstockCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年10月19日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.action.v1.dto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockCollection;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockCollectionDetail;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockCollectionMaterial;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockGather;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockGatherDetail;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockLatestCollection;
import com.yuexunit.finance.bulkstock.action.v1.model.BulkstockLatestGather;
import com.yuexunit.finance.bulkstock.entity.BulkstockCategoryEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockCollectionEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockCollectionMaterialEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockGatherEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockLatestCollectionEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockLatestGatherEntity;
import com.yuexunit.finance.bulkstock.entity.lexicon.MeasurementEnum;
import com.yuexunit.finance.bulkstock.service.BulkstockCategoryService;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.fs.parse.service.FileParseService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: BulkstockCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年10月19日 下午2:12:57
 */
public class BulkstockCastor {

	private static List<String> COLLECTION_TITLES = Arrays.asList(new String[] { "单据号", "商品类别", "商品名称", "商品编号", "规格型号", "产地", "生产厂家", "单位", "价格", "片区", "所属省份", "所属城市", "所属区县", "起始日期", "终止日期", "价格日期" });

	private static List<String> GATHER_TITLES = Arrays.asList(new String[] { "商品类别", "商品名称", "规格型号", "价格", "周收盘价", "七日均价", "价格日期" });

	public static BulkstockCollectionEntity fromBulkstockCollection(BulkstockCollection info, String... ignores) {
		try {
			BulkstockCollectionEntity entity = new BulkstockCollectionEntity();
			BeanUtils.copyProperties(info, entity, ignores);
			entity.setMeasurement(MeasurementEnum.valueOf(info.getMeasurement()));
			entity.setCollectionDate(DateUtil.parseDateInt(info.getCollectionDate()));
			entity.setBeginDate(DateUtil.parseDateInt(info.getBeginDate()));
			entity.setEndDate(DateUtil.parseDateInt(info.getEndDate()));
			return entity;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("价格信息生成失败");
		}
	}

	public static BulkstockCollectionDetail toBulkstockCollectionDetail(BulkstockCollectionEntity entity, String... ignores) {
		try {
			BulkstockCollectionDetail detail = new BulkstockCollectionDetail();
			BeanUtils.copyProperties(entity, detail, ignores);
			detail.setMeasurement(entity.getMeasurement().ordinal());
			detail.setCollectionDate(DateUtil.formatDate(entity.getCollectionDate()));
			detail.setBeginDate(DateUtil.formatDate(entity.getBeginDate()));
			detail.setEndDate(DateUtil.formatDate(entity.getEndDate()));
			return detail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("价格信息生成失败");
		}
	}

	public static List<BulkstockCollectionEntity> fromCollectionMaterial(String fileUuid, BulkstockCollectionEntity base) {
		FileParseService fileParseService = ComponentFactory.loadClass(FileParseService.class);
		BulkstockCategoryService bulkstockCategoryService = ComponentFactory.loadClass(BulkstockCategoryService.class);
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);

		Date date = DateUtils.addDateField(new Date(), Calendar.DATE, -30);
		Integer earliest = DateUtils.parseDateInt(date);

		List<List<String>> datas = fileParseService.parseExcel(fileUuid, COLLECTION_TITLES);
		List<BulkstockCollectionEntity> entities = new ArrayList<>();
		try {
			for (List<String> l : datas) {
				System.out.println(earliest);
				System.out.println(l.get(15));
				if (l.get(15) == null || StringUtils.isBlank(l.get(9))) {
					continue;
				}
				if (earliest > Integer.parseInt(l.get(15))) {
					continue;
				}
				BulkstockCollectionEntity entity = base.clone();
				entity.setCollectionNumber(l.get(0));
				entity.setCategoryName(l.get(1));
				entity.setBulkstockName(l.get(2));
				entity.setBulkstockNumber(l.get(3));
				entity.setSpecification(l.get(4));
				entity.setOrigin(l.get(5));
				entity.setManufacturer(l.get(6));
				entity.setUnit(l.get(7));
				entity.setPrice(new BigDecimal(Double.parseDouble(l.get(8))));
				entity.setRegionCategory(l.get(9));
				entity.setRegion(l.get(9));
				entity.setProvince(l.get(10));
				entity.setCity(l.get(11));
				entity.setDistrict(l.get(12));
				entity.setBeginDate(Integer.parseInt(l.get(13)));
				entity.setEndDate(Integer.parseInt(l.get(14)));
				entity.setCollectionDate(Integer.parseInt(l.get(15)));
				BulkstockCategoryEntity category = bulkstockCategoryService.inquireBulkstockCategory(entity.getCategoryName());
				if (category != null) {
					entity.setCategoryId(category.getBulkstockCategoryId());
					BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(entity.getCategoryId(), entity.getBulkstockName(), entity.getSpecification());
					if (bulkstockInfo != null) {
						entity.setBulkstockInfoId(bulkstockInfo.getBulkstockInfoId());
						entity.setMeasurement(bulkstockInfo.getMeasurement());
					}
				}
				if (entity.getBulkstockInfoId() != null) {
					entities.add(entity);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entities;
	}

	public static BulkstockCollectionMaterial toBulkstockCollectionMaterial(BulkstockCollectionMaterialEntity entity, String... ignores) {
		try {
			BulkstockCollectionMaterial detail = new BulkstockCollectionMaterial();
			BeanUtils.copyProperties(entity, detail, ignores);
			detail.setCollectionDate(DateUtil.formatDate(entity.getCollectionDate()));
			return detail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("采集信息生成失败");
		}
	}

	public static List<BulkstockGatherEntity> fromBulkstockGatherMaterial(String fileUuid) {
		FileParseService fileParseService = ComponentFactory.loadClass(FileParseService.class);
		BulkstockCategoryService bulkstockCategoryService = ComponentFactory.loadClass(BulkstockCategoryService.class);
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);

		Date date = DateUtils.addDateField(new Date(), Calendar.DATE, -30);
		Integer earliest = DateUtils.parseDateInt(date);

		List<List<String>> datas = fileParseService.parseExcel(fileUuid, GATHER_TITLES);
		List<BulkstockGatherEntity> entities = new ArrayList<BulkstockGatherEntity>();
		try {
			for (List<String> l : datas) {
				if (l.get(6) == null) {
					continue;
				}
				if (earliest > Integer.parseInt(l.get(6))) {
					continue;
				}

				BulkstockGatherEntity entity = new BulkstockGatherEntity();
				entity.setCategoryName(l.get(0));
				entity.setBulkstockName(l.get(1));
				entity.setSpecification(l.get(2));
				entity.setPrice(new BigDecimal(l.get(3)));
				entity.setClosingPrice(new BigDecimal(l.get(4)));
				entity.setWeekAvgPrice(new BigDecimal(l.get(5)));
				entity.setPriceDate(DateUtils.parseDateInt(l.get(6)));

				BulkstockCategoryEntity category = bulkstockCategoryService.inquireBulkstockCategory(entity.getCategoryName());
				if (category != null) {
					entity.setCategoryId(category.getBulkstockCategoryId());
					BulkstockInfoEntity bulkstockInfo = bulkstockInfoService.inquireBulkstockInfo(entity.getCategoryId(), entity.getBulkstockName(), entity.getSpecification());
					if (bulkstockInfo != null) {
						entity.setBulkstockInfoId(bulkstockInfo.getBulkstockInfoId());
						entity.setMeasurement(bulkstockInfo.getMeasurement());
					}
				}
				if (entity.getBulkstockInfoId() != null) {
					entities.add(entity);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return entities;
	}

	public static BulkstockGatherEntity fromBulkstockGather(BulkstockGather info) {
		BulkstockGatherEntity entity = new BulkstockGatherEntity();
		entity.setMeasurement(MeasurementEnum.valueOf(info.getMeasurement()));
		entity.setBulkstockName(info.getBulkstockName());
		entity.setCategoryId(info.getCategoryId());
		entity.setClosingPrice(new BigDecimal(info.getClosingPrice()));
		entity.setPrice(new BigDecimal(info.getPrice()));
		entity.setPriceDate(DateUtils.parseDateInt(info.getPriceDate()));
		entity.setSpecification(info.getSpecification());
		entity.setWeekAvgPrice(new BigDecimal(info.getWeekAvgPrice()));
		return entity;
	}

	public static BulkstockGatherDetail toBulkstockGatherDetail(BulkstockGatherEntity entity) {
		BulkstockGatherDetail detail = new BulkstockGatherDetail();
		detail.setMeasurement(entity.getMeasurement().ordinal());
		detail.setGatherId(entity.getGatherId());
		detail.setCategoryId(entity.getCategoryId());
		detail.setBulkstockName(entity.getBulkstockName());
		detail.setSpecification(entity.getSpecification());
		detail.setPrice(entity.getPrice().toString());
		detail.setPriceDate(DateUtil.formatDate(entity.getPriceDate()));
		detail.setRiseFall(entity.getRiseFall() == null ? "" : entity.getRiseFall().toString());
		detail.setIncrease(entity.getIncrease() == null ? "" : entity.getIncrease().toString());
		detail.setClosingPrice(entity.getClosingPrice().toString());
		detail.setWeekAvgPrice(entity.getWeekAvgPrice().toString());
		detail.setAuditor(entity.getAuditor());
		detail.setAuditState(entity.getAuditState().ordinal());
		detail.setIssueDateTime(entity.getIssueDateTime());
		return detail;
	}

	public static BulkstockLatestGather toBulkstockLatestGather(BulkstockLatestGatherEntity entity) {
		BulkstockLatestGather info = new BulkstockLatestGather();
		BeanUtils.copyProperties(entity, info);
		info.setMeasurement(entity.getMeasurement().ordinal());
		info.setClosingPrice(entity.getClosingPrice().toString());
		info.setWeekAvgPrice(entity.getWeekAvgPrice().toString());
		info.setIncrease(entity.getIncrease().toString());
		info.setPrice(entity.getPrice().toString());
		info.setRiseFall(entity.getRiseFall().toString());
		return info;
	}

	public static BulkstockLatestCollection toBulkstockLatestCollection(BulkstockLatestCollectionEntity entity) {
		BulkstockLatestCollection info = new BulkstockLatestCollection();
		BeanUtils.copyProperties(entity, info);
		info.setMeasurement(entity.getMeasurement().ordinal());
		info.setIncrease(entity.getIncrease().toString());
		info.setPrice(entity.getPrice().toString());
		info.setRiseFall(entity.getRiseFall().toString());
		return info;
	}
}
